﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using EPiServer.Web.WebControls;

namespace EPiServer.Municipality.Templates.Pages {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    public partial class DocumentList : TemplatePage {
        /* *******************************************************************
         * Properties
         * *******************************************************************/
        #region private string DocumentRoot
        /// <summary>
        /// Gets the DocumentRoot of the DocumentList
        /// </summary>
        /// <value></value>
        private string DocumentRoot {
            get {
                return (string)CurrentPage["DocumentRoot"] ?? String.Empty;
            }
        }
        #endregion
        /* *******************************************************************
         * Methods
         * *******************************************************************/
        #region override protected void OnInit(EventArgs e)
        override protected void OnInit(EventArgs e) {
            InitializeComponent();
            base.OnInit(e);
        }
        #endregion
        #region private void InitializeComponent()
        /// <summary>
        /// 
        /// </summary>
        private void InitializeComponent() {
            Load += Page_Load;
        }
        #endregion
        #region protected string ConvertFileSize(long FileSize)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileSize"></param>
        /// <returns></returns>
        protected string ConvertFileSize(long fileSize) {
            if (fileSize < 1024) {
                return fileSize + " bytes";
            }
            if ((fileSize >= 1024) && (fileSize < 1024000)) {
                return Convert.ToInt16(fileSize / 1024) + " kb";
            }
            return Math.Round(((1.0 * fileSize) / 1024000), 2) + " Mb";
        }

        #endregion
        #region protected string WrapContent(string content, string wrapElement)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="wrapElement"></param>
        /// <returns></returns>
        protected string WrapContent(string content, string wrapElement) {
            if (string.IsNullOrEmpty((content)) || string.IsNullOrEmpty(wrapElement))
                return content;
            return string.Format("<{0}>{1}</{0}>", wrapElement, content);
        }
        #endregion
        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
        #region protected void Page_Load(object sender, EventArgs e)
        /// <summary>
        /// This method is called when the Page's Load event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> of the event.</param>
        protected void Page_Load(object sender, EventArgs e) {
            ((MasterPages.Default)Page.Master).CssClass = "documents";
            FileList.DataSource = new FileSystemDataSource() { Root = DocumentRoot };
            FileList.DataBind();
        }
        #endregion
    }
}